
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from settings import Ui_Dialog

from utes import dumpClickers,logit

def setmydata(table,mystruct):
    table.setRowCount(len(mystruct))
    table.setColumnCount(2)
    for row,item in enumerate(mystruct):
        newitem = QTableWidgetItem(item.name())
        newitem.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable)
        if QSettings().value("/clickfu/set/"+item.name()).toBool():
            newitem.setCheckState(Qt.Checked)
        else:
            newitem.setCheckState(Qt.Unchecked)
        
        table.setItem(row, 0, newitem)
        desc = QTableWidgetItem(item.desc())
        desc.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
        table.setItem(row, 1, desc)
    hv = table.horizontalHeader()
    hv.setStretchLastSection(True)
    table.resizeColumnsToContents()

class Dialog(QDialog):
    def __init__(self,clickActions):
      QDialog.__init__(self)
      self.ui = Ui_Dialog()
      self.ui.setupUi(self)

      logit("dialog setup")
      dumpClickers(clickActions)

      self.clickActions = clickActions
		# set the delays
      value = QSettings().value("/clickfu/delay1",QVariant(0.0)).toDouble()[0]
      self.ui.delay1.setValue(value)
      value = QSettings().value("/clickfu/delay2",QVariant(0.0)).toDouble()[0]
      self.ui.delay2.setValue(value)

		# now set the table
      setmydata(self.ui.clickfuTable,clickActions)
      QObject.connect(self.ui.moveUp,SIGNAL("clicked()"),self.moveUp)
      QObject.connect(self.ui.moveDown,SIGNAL("clicked()"),self.moveDown)

    def swapRows(self,i,j):
        t1 = [self.ui.clickfuTable.takeItem(i,0),self.ui.clickfuTable.takeItem(i,1)]
        t2 = [self.ui.clickfuTable.takeItem(j,0),self.ui.clickfuTable.takeItem(j,1)]
            
        self.ui.clickfuTable.setItem(j,0,t1[0])
        self.ui.clickfuTable.setItem(j,1,t1[1])
        self.ui.clickfuTable.setItem(i,0,t2[0])
        self.ui.clickfuTable.setItem(i,1,t2[1])

        t1[0].setSelected(True)
        t1[1].setSelected(True)

        t2[0].setSelected(False)
        t2[1].setSelected(False)
        

    def moveUp(self):
        row =  self.ui.clickfuTable.currentRow()
        if row > 0:
            self.swapRows(row,row-1)
            self.ui.clickfuTable.setCurrentCell(row-1,0)

    def moveDown(self):
        row = self.ui.clickfuTable.currentRow()
        if row < self.ui.clickfuTable.rowCount()-1:
            self.swapRows(row,row+1)
            self.ui.clickfuTable.setCurrentCell(row+1,0)


    def accept(self):
        logit("accepting dialog")
        dumpClickers(self.clickActions)

        for row in range(self.ui.clickfuTable.rowCount()):
            logit(row)
            namebox = self.ui.clickfuTable.item(row,0)
            ch = namebox.checkState()
            v = namebox.text()
            if ch == Qt.Checked:
                logit("checked "+v)
                QSettings().setValue("/clickfu/set/"+v,True)
            else:
                QSettings().setValue("/clickfu/set/"+v,False)


        QSettings().setValue("/clickfu/delay1",self.ui.delay1.value())
        QSettings().setValue("/clickfu/delay2",self.ui.delay2.value())
        QDialog.accept(self)
        
